import { createClient } from 'redis'
const config = require('config')
const REDIS = config.get('REDIS')

/*
数据库定义:
1用户配置
*/


// 定义数据库类
export default class MyRedis {
  // 实例化数据库的时候需要传入链接哪个数据库
  constructor(private db=REDIS.dataBase){}
  // 数据库配置
  private option = {
    url: `redis://:${REDIS.password}@${REDIS.host}:${REDIS.post}/${this.db}`
  }
  // 创建数据库实例
  private client = createClient(this.option);

  // 链接数据库
  public async connect() {
    await this.client.connect()
  }

  // 断开数据库链接
  public async disconnect() {
    await this.client.disconnect()
  }

  // 获取字符串数据
  public async get(key: string) {
    return await this.client.get(key)
  }

  // 写入字符串数据
  public async set(key: string, value: string) {
    return await this.client.set(key, value)
  }

  // 获取哈希字符串
  public async hGet(key: string, field: string) {
    return await this.client.hGet(key, field)
  }
  // 写入哈希字符串
  public async hSet(key: string, field: string, value: string) {
    await this.client.hSet(key, field, value)
  }
}